﻿namespace DotRas
{
    using pptpclient.Properties;
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Globalization;

    [Serializable, DebuggerDisplay("Name = {Name}, DeviceType = {DeviceType}")]
    public sealed class RasDevice
    {
        private string _deviceType;
        private string _name;

        private RasDevice(string name, string deviceType)
        {
            this._name = name;
            this._deviceType = deviceType;
        }

        public static RasDevice Create(string name, string deviceType)
        {
            if (string.IsNullOrEmpty(name))
            {
                DotRas.ThrowHelper.ThrowArgumentException("name", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(deviceType))
            {
                DotRas.ThrowHelper.ThrowArgumentException("deviceType", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            return new RasDevice(name, deviceType);
        }

        public static RasDevice GetDeviceByName(string name, string deviceType)
        {
            return GetDeviceByName(name, deviceType, false);
        }

        public static RasDevice GetDeviceByName(string name, string deviceType, bool exactMatchOnly)
        {
            foreach (RasDevice device2 in GetDevices())
            {
                if (string.Compare(deviceType, device2.DeviceType, true, CultureInfo.CurrentCulture) == 0)
                {
                    if (!exactMatchOnly && device2.Name.ToLower(CultureInfo.CurrentCulture).Contains(name.ToLower(CultureInfo.CurrentCulture)))
                    {
                        return device2;
                    }
                    if (exactMatchOnly && (string.Compare(name, device2.Name, false, CultureInfo.CurrentCulture) == 0))
                    {
                        return device2;
                    }
                }
            }
            return null;
        }

        public static ReadOnlyCollection<RasDevice> GetDevices()
        {
            return RasHelper.GetDevices();
        }

        public static ReadOnlyCollection<RasDevice> GetDevicesByType(string deviceType)
        {
            if (string.IsNullOrEmpty(deviceType))
            {
                DotRas.ThrowHelper.ThrowArgumentException("deviceType", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            Collection<RasDevice> list = new Collection<RasDevice>();
            foreach (RasDevice device in GetDevices())
            {
                if (string.Compare(device.DeviceType, deviceType, true, CultureInfo.CurrentCulture) == 0)
                {
                    list.Add(device);
                }
            }
            return new ReadOnlyCollection<RasDevice>(list);
        }

        public string DeviceType
        {
            get
            {
                return this._deviceType;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
}

